<html>
<head>
    <meta charset="utf-8">
    <title>分类编辑</title>
    <#assign base=request.contextPath  />
    <#setting  number_format="0.##"/>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${base}/backstage/layui/css/layui.css" media="all">
    <script src="${base}/sweep/js/mui.min.js"></script>
    <script src="${base}/backstage/layui/layui.js"></script>
    <script src="${base}/backstage/wangEdit/wangEditor.js"></script>
    <script src="${base}/backstage/js/jquery-1.8.3.min.js"></script>
    <script>
        var base =  "${base}";
    </script>
    <script src="${base}/backstage/js/ipNameUrl.js"></script>
</head>
<body>
<form class="layui-form " action="" style="padding-left: 15px;">
    <input type="hidden" value="${classification.id}" id="id"/>
<br>
<br>
    <div class="layui-form-item">
        <label class="layui-form-label">商品分类</label>
        <div class="layui-input-block">
            <select name="modules" id="classificationId" lay-verify="required" lay-search="">
                <option value="">直接选择商品分类</option>
                <#list commodityClassification as commodityClassification>
                    <option value="${commodityClassification.id}">${commodityClassification.name}</option>
                </#list>

            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">类型</label>
        <div class="layui-input-block">
            <input class="layui-input" name="name" value="${classification.type}" id="type" placeholder="请输入类型" autocomplete="off">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">分类名称</label>
        <div class="layui-input-block">
            <input class="layui-input" name="name" value="${classification.name}" id="name" placeholder="请输入商品分类名称" autocomplete="off">
        </div>
    </div>
    <br>
    <div class="layui-form-item">
        <label class="layui-form-label">图片</label>
        <div class="layui-input-block">
            <div class="layui-upload-drag" id="test10">
                <div id="file1Div">
                    <img src="${classification.url}" style="width: 342px;height: 136px">
                </div>
                <i class="layui-icon"></i>
                <p>点击下传，或将文件拖拽到此处</p>
            </div>
        </div>
    </div>

<br>
<br>
    <div align="center">
        <button type="button" class="layui-btn" id="saveButton">保存</button>
        <button type="button" class="layui-btn" onclick="window.history.back(-1);">返回</button>
    </div>
    <br>
    <br>
</form>

</body>
<script>
    var param = "";
    layui.use(['form', 'layer', 'upload'], function () {
        var form = layui.form
            , layer = layui.layer
            , $ = layui.jquery
            , upload = layui.upload;

        //拖拽下传
        upload.render({
            elem: '#test10'
            , url: '${base}/admin/file/uploadFile'
            , before: function (obj) {
                layer.load(); //下传loading
            }
            , done: function (res) {
                layer.closeAll('loading'); //关闭loading
                //如果下传失败
                if (res.code > 0) {
                    return layer.msg('上传失败');
                }
                $('#file1Div').html("")
                $('#file1Div').append('<img src="' + res.data + '" style="width: 342px;height: 136px">'); //图片链接（base64）
                param += res.data;
                return layer.msg('上传成功');
            }
        });

        $.ajax({
            type: "GET",
            url: "${base}/admin/classification/getClassificationId",
            data: {id:$("#id").val()},
            success: function (data) {
                if (data.code === 200) {
                    $("select option[value='" + data.data.classificationId + "']").attr("selected", "selected");
                    layui.form.render(); //更新全部
                }
            }
        });


    })

    $("#saveButton").on("click", function () {


        if ($("#classificationId").val()=="") {
            layer.msg("请填写商品分类名称！")
            return;
        }
        if ($("#name").val()=="") {
            layer.msg("请填写分类名称！")
            return;
        }
        if ($("#type").val()=="") {
            layer.msg("请填写类型！")
            return;
        }
        if (param=="") {
            layer.msg("请上传图片")
            return;
        }
        var formDate;
        if (param != "") {
            formDate = {
                name: $("#name").val(),
                type: $("#type").val(),
                classificationId: $("#classificationId").val(),
                url: param,
                id: $("#id").val()
            }
        } else {
            formDate = {
                name: $("#name").val(),
                type: $("#type").val(),
                classificationId: $("#classificationId").val(),
                id: $("#id").val()
            }
        }

        $.ajax({
            type: "POST",
            contentType: "application/json;charset=UTF-8",
            url: "${base}/admin/classification/updateById",
            data: JSON.stringify(formDate),
            success: function (data) {
                if (data.code === 200) {
                    layer.msg(data.msg)
                    window.location.href = "${base}/admin/classification/classification.html"
                } else {
                    layer.msg(data.msg)
                }
            }
        });
    })
</script>
</html>


